#!/bin/bash

plain='\033[0m'
info='\033[32m'
error='\033[41;30m'
success='\033[42;30m'

# 检查hosts文件
if [ ! -f "$pwd/k8s/hosts.template" ]; then
	echo -e "${error} [ERROR]: File does not exist! ${plain}"
    exit 1
fi

# 读取配置文件
IPADDRESS=()
HOSTNAMES=()
while read line; do
        ip_addres=$(echo $line | cut -d " " -f1)
		host_name=$(echo $line | cut -d " " -f2)
        IPADDRESS[${#IPADDRESS[*]}]=${ip_addres}
		HOSTNAMES[${#HOSTNAMES[*]}]=${host_name}
done < $pwd/k8s/hosts.template

ssh_keygen(){
# 登陆到各个主机上，使用ssh-keygen工具生成公钥和私钥
yum -y install expect &>/dev/null
echo -e "${info} [2:1/7] Use ssh-keygen to generate key... ${plain}"

for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}

        echo "IP:${ip_addres}"		
        expect <<EOF
                spawn ssh root@${ip_addres} "rm -rf  ~/.ssh; ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q"
                expect {
                        "yes/no" { send "yes\n";exp_continue}     
                        "password" { send "$password\n"}
                }
                expect eof 
EOF
done &>/dev/null
}

scp_public_keys(){
# 将每个主机上的id_rsa.pub拷贝到本地，并汇总至authorized_keys
echo -e "${info} [2:2/7] Copy remote public_keys to local... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}

        echo "IP:${ip_addres}" 
        TMP_FILE="$pwd/k8s/id_rsa.pub.${ip_addres}"
        expect <<EOF
                spawn scp root@${ip_addres}:~/.ssh/id_rsa.pub  $TMP_FILE
                expect {
                        "yes/no" { send "yes\n";exp_continue}     
                        "password" { send "$password\n"}
                }
                expect eof 
EOF
        cat $TMP_FILE >> ~/.ssh/authorized_keys && rm -f $TMP_FILE
done &>/dev/null
}

scp_authorized_keys(){
# 将本地authorized_keys分发到每个主机上
echo -e "${info} [2:3/7] Send local authorized_keys to each host... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}

        echo "IP:${ip_addres}"
        expect <<EOF
                spawn scp /root/.ssh/authorized_keys root@${ip_addres}:/root/.ssh/authorized_keys  
                expect {
                        "yes/no" { send "yes\n";exp_continue}     
                        "password" { send "$password\n"}
                }
                expect eof 
EOF
done &>/dev/null
}

scp_known_hosts(){
# 将本地known_hosts分发到每个主机上
echo -e "${info} [2:4/7] Send local known_hosts to each host... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}

        echo "IP:${ip_addres}"
		scp /root/.ssh/known_hosts root@${ip_addres}:/root/.ssh/known_hosts
done &>/dev/null
}

scp_hosts_template(){
# 将hosts模板分发到每个主机上
echo -e "${info} [2:5/7] Send hosts.template to each host... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}
		host_name=${HOSTNAMES[$i]}
		
        echo "IP:${ip_addres} hostname:${host_name}"
		scp $pwd/k8s/hosts.template root@${ip_addres}:/etc/hosts.template
		ssh root@${ip_addres} "cat /etc/hosts.template >> /etc/hosts && rm -rf /etc/hosts.template && hostnamectl set-hostname --static ${host_name}"
done &>/dev/null
}

ssh_hostname(){
# ssh连接到所有hostname，生成hostname的known_hosts记录
echo -e "${info} [2:6/7] SSH to each host with the hostname... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}
		host_name=${HOSTNAMES[$i]}
		
        echo "IP:${ip_addres} hostname:${host_name}"
        expect <<EOF
                spawn ssh root@${host_name} "exit"
                expect {
                        "yes/no" { send "yes\n";exp_continue}
                }
                expect eof 
EOF
done &>/dev/null
}

scp_known_hosts_again(){
# 再次将本地known_hosts分发到每个主机上
echo -e "${info} [2:7/7] Send local known_hosts to each host again... ${plain}"
for ((i = 0; i < ${#IPADDRESS[@]}; i++)); do
        ip_addres=${IPADDRESS[$i]}
		host_name=${HOSTNAMES[$i]}
		
        echo "IP:${ip_addres} hostname:${host_name}"
		scp /root/.ssh/known_hosts root@${host_name}:/root/.ssh/known_hosts
done &>/dev/null
echo -e "${success} [INFO]:SSH Auth SUCCESS! ${plain}"
}


ssh_keygen
scp_public_keys
scp_authorized_keys
scp_known_hosts
scp_hosts_template
ssh_hostname
scp_known_hosts_again